package controllers.administrator;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import services.AvailableItemService;
import services.CommentService;
import services.CustomerService;
import services.OrderService;
import controllers.AbstractController;
import domain.Customer;
import domain.Dashboard;

@Controller
@RequestMapping("/dashboard/administrator")
public class DashboardAdministratorController extends AbstractController {
	@Autowired
	private AvailableItemService availableItemService;

	@Autowired
	private CustomerService customerService;

	@Autowired
	private OrderService orderService;

	@Autowired
	private CommentService commentService;

	public DashboardAdministratorController() {
		super();
	}

	// Listing methods
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView list() {
		ModelAndView result;
		Dashboard dashboard;
		Integer numCustomers;
		Integer numAvailableItems;
		Integer numOrders;
		Double totalAmountOfAllOrders;
		Collection<Customer> buyersMoreSpent;
		Collection<Customer> sellersMoreEarned;
		Integer numComments;

		dashboard = new Dashboard();

		numCustomers = customerService.findAll().size();
		numAvailableItems = availableItemService.findAvailableItems().size();
		numOrders = orderService.findAll().size();
		totalAmountOfAllOrders = orderService.findTotalAmount();
		buyersMoreSpent = customerService.findMoreSpentByBuyer();
		sellersMoreEarned = customerService.findMoreEarnedBySeller();
		numComments = commentService.findAll().size();

		dashboard.setBuyersMoreSpent(buyersMoreSpent);
		dashboard.setNumAvailableItems(numAvailableItems);
		dashboard.setNumComments(numComments);
		dashboard.setNumCustomers(numCustomers);
		dashboard.setNumOrders(numOrders);
		dashboard.setSellersMoreEarned(sellersMoreEarned);
		dashboard.setTotalAmountOfAllOrders(totalAmountOfAllOrders);

		result = new ModelAndView("dashboard/list");
		result.addObject("requestURI", "dashboard/administrator/list.do");
		result.addObject("dashboard", dashboard);
		result.addObject("backURI", "/welcome/index.do");
		return result;
	}
}